package com.neusoft.chanceserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.neusoft.chanceserver.dto.CompetitionAdd;
import com.neusoft.chanceserver.dto.RespBean;
import com.neusoft.chanceserver.entity.ChanceCompetition;
import com.neusoft.chanceserver.entity.Competition;
import com.neusoft.chanceserver.mapper.ChanceCompetitionMapper;
import com.neusoft.chanceserver.mapper.CompetitionMapper;
import com.neusoft.chanceserver.service.ICompetitionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 竞争情况表 服务实现类
 * </p>
 *
 * @author liuxp
 * @since 2021-07-13
 */
@Service
public class CompetitionServiceImpl extends ServiceImpl<CompetitionMapper, Competition> implements ICompetitionService {

    @Autowired
    CompetitionMapper competitionMapper;

    @Autowired
    ChanceCompetitionMapper chanceCompetitionMapper;

    @Override
    public RespBean addCompetition(CompetitionAdd competitionAdd) {
        RespBean respBean = RespBean.ok(200, "Competition successfully added!");
        competitionMapper.insert(competitionAdd.getCompetition());
        int competitionId = competitionAdd.getCompetition().getCompetitionId();
        ChanceCompetition chanceCompetition = new ChanceCompetition();
        chanceCompetition.setChanceId(competitionAdd.getChanceId());
        chanceCompetition.setCompetitionId(competitionId);
        chanceCompetitionMapper.insert(chanceCompetition);
        return respBean;
    }

    @Override
    public RespBean updateCompetition(Competition competition) {
        RespBean respBean = RespBean.ok(200, "Competition updated successfully!");
        competitionMapper.updateById(competition);
        return respBean;
    }

    @Override
    public RespBean deleteCompetition(Integer competitionId) {
        RespBean respBean = RespBean.ok(200, "Competition deleted successfully!");
        ChanceCompetition chanceCompetition = chanceCompetitionMapper.selectById(competitionId);
        competitionMapper.deleteById(competitionId);
        LambdaQueryWrapper<ChanceCompetition> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ChanceCompetition::getCompetitionId, competitionId);
        chanceCompetitionMapper.delete(wrapper);
        return respBean;
    }

    @Override
    public RespBean searchCompetitionByChanceId(Integer chanceId) {
        List<Integer> competitionIds = new ArrayList<>();
        LambdaQueryWrapper<ChanceCompetition> chanceCompetitionLambdaQueryWrapper = Wrappers.lambdaQuery();
        chanceCompetitionLambdaQueryWrapper.eq(ChanceCompetition::getChanceId, chanceId);
        List<ChanceCompetition> chanceCompetitions = chanceCompetitionMapper.selectList(chanceCompetitionLambdaQueryWrapper);
        for(ChanceCompetition chanceCompetition : chanceCompetitions){
            competitionIds.add(chanceCompetition.getCompetitionId());
        }
        List<Competition> competitions = new ArrayList<>();
        if(competitionIds.size() > 0)
            competitions = competitionMapper.selectBatchIds(competitionIds);
        RespBean respBean = RespBean.ok(200, "Find " + competitions.size() + " competitions!");
        respBean.setObj(competitions);
        return respBean;
    }
}
